<template>
  <div>
    <van-search
      v-model="params.name"
      placeholder="请输入所属市场名称"
      @search="onSearch"
      @clear="clearSearch"
    />
    <div class="page_space">
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        @load="onLoad"
      >
        <base-card
          v-for="(item, index) in list"
          :key="index"
          :data="item.data"
          :title="item.title"
          class="mt-15"
          @click="hitThis(item)"
        />
      </van-list>
    </div>
  </div>
</template>

<script>
import baseCard from '@/components/base-card'
import { selectMarket } from '@/api/subject'
import { store, mutations } from '@/observable'

export default {
  name: 'selectMarket',
  components: { baseCard },
  data() {
    return {
      loading: false,
      finished: false, // 触底加载结束
      params: {
        limit: 10,
        page: 1
      },
      list: []
    }
  },
  created() {},
  methods: {
    clearSearch() {
      this.params.name = ''
      this.getList(this.params, true)
    },
    onSearch() {
      this.getList(this.params, true)
    },
    // 选中
    hitThis(e) {
      mutations.setMarketInfo({
        managementEntName: e.title,
        managementEntId: e.id
      })
      this.$router.go(-1)
    },
    // 触底加载
    onLoad() {
      this.getList(this.params)
    },
    // 选择采样人员
    async getList(params, isReset) {
      if (isReset) {
        this.loading = true
        this.finished = false
        this.list = []
        this.params.page = 1
      }
      const res = await selectMarket(params)
      if (res.code === '0') {
        res.data.content.forEach((item) => {
          this.list.push({
            title: item.name,
            id: item.id,
            data: [
              {
                key: '市场名称',
                value: item.name
              }
            ]
          })
        })
        // 加载状态结束
        this.loading = false
        this.params.page++
        // 数据全部加载完成
        if (res.data.last) {
          this.finished = true
        }
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.empty_tip {
  text-align: center;
  margin-top: 50px;

  font-family: PingFang SC Medium;
  font-size: 14px;
  font-weight: normal;
  line-height: 19px;
  letter-spacing: 0px;
  color: #b2b8bd;
}
</style>
